const crypto = require('crypto');
const Response = require('./Response')
const {
	getTime,
} = require('../utils/dateUtils')
const {
	_UltDB,
	_WebDB,
} = global

const _TableName = 'web_song_list'
const _Feild = [
	'list_name',
	'original_create_date',
	'original_create_by',
	// 'favorites_count',
	// 'share_count',
	// 'comment_count',
	// 'play_count',
	'profile',
	// 'hot',
	// 'news',
	'list_id',
	'url_source',
	'modify_date'
]

const getNum = (function() {
	let _num = 0
	return () => _num += 1
})()

module.exports = {

	/**
	 * 歌单列表
	 * @param  {[type]} params [description]
	 * @return {[type]}        [description]
	 */
	list: async function(params) {
		let {
			source,
			pn = 1,
			ps = 1000,
		} = params
		let where = (source && source != 'all') ? ` AND wsl.url_source = '${source}' ` : ''
		let SQL = `SELECT count(1) AS total FROM ${_TableName} wsl WHERE 1 = 1 ${where}`
		let {
			total
		} = await _WebDB.selectOne(SQL)
		let skip = (pn - 1) * ps
		SQL = 'SELECT wsl.list_id, wsl.list_name, wsl.profile, wsl.url_source, GROUP_CONCAT(distinct t.tag_name separator "、") AS tag FROM web_song_list wsl '
		SQL += ' LEFT JOIN web_song_list_tag wslt ON wslt.list_id = wsl.list_id '
		SQL += ' LEFT JOIN tag t ON wslt.tag_id = t.tag_id '
		SQL += ` WHERE 1 = 1 ${where} GROUP BY wsl.list_id LIMIT ${skip}, ${ps}`
		// SQL += ` WHERE wsl.list_id = '814038416' GROUP BY wsl.list_id LIMIT ${skip}, ${ps}`
		let docs = await _WebDB.select(SQL)
		return new Response('000', '成功', {
			total,
			docs,
		})
	},

	/**
	 * 歌单歌曲(爱听对应)
	 * @param  {[type]} params [description]
	 * @return {[type]}        [description]
	 */
	items: async function(params) {
		let {
			list_id,
			source,
		} = params
		let SQL = `SELECT DISTINCT ws.itemid FROM web_song_list_songs wsls LEFT JOIN web_songs ws ON ws.song_id = wsls.song_id WHERE wsls.list_id = '${list_id}' AND wsls.url_source = '${source}' AND ws.itemid`
		let itemidList = await _WebDB.select(SQL)
		let docs = []
		for (let i = 0; i < itemidList.length; i++) {
			let {
				itemid,
			} = itemidList[i]
			let SQL = 'SELECT it.itemid AS song_id, it.Name AS song_name, GROUP_CONCAT(distinct da.name separator "、") AS artist, al.name AS album, it.Exclusivity AS exclusivity, cpi.Name AS cp '
			SQL += ' FROM iteminfo it '
			SQL += ' LEFT JOIN itemartist ia ON ia.ItemID = it.itemid '
			SQL += ' LEFT JOIN itemalbum iab ON iab.itemid = it.itemid '
			SQL += ' LEFT JOIN albuminfo al ON al.albumid = iab.albumid '
			SQL += ' LEFT JOIN dim_artist da ON da.artistid = ia.ArtistID '
			SQL += ' LEFT JOIN contentproviderinfo cpi ON cpi.cpid = it.cpid '
			SQL += ` WHERE it.itemid = '${itemid}' GROUP BY it.itemid`
			let info = await _UltDB.selectOne(SQL)
			docs.push(info)
		}
		return new Response('000', '成功', {
			docs,
		})
	},

	explist: async function(params) {
		let {
			pn = 1,
				ps = 1000,
		} = params
		let SQL = `SELECT count(1) AS total FROM exp_ultimate`
		let {
			total
		} = await _WebDB.selectOne(SQL)
		let skip = (pn - 1) * ps
		SQL = 'SELECT num, list_name, tag, profile, itemid, itemname, artist, album, cp, exclusivity, url_source, modify_date FROM exp_ultimate '
		SQL += ` LIMIT ${skip}, ${ps}`
		let docs = await _WebDB.select(SQL)
		return new Response('000', '成功', {
			total,
			docs,
		})

	},

}

/**
 * 密码加密
 * @param  {[type]} password [description]
 * @return {[type]}          [description]
 */
function encPwd(password) {
	return crypto.createHash('md5').update(password).digest('hex');
}